package com.lxmes.gs.controller;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.projectOrganizationVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.ProjectOrganization;
import com.lxmes.gs.service.IProjectOrganizationService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 项目组织Controller
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "项目组织")    
@RestController
@RequestMapping("/gs/projectOrganization")
public class ProjectOrganizationController extends BaseController{
    @Autowired
    private IProjectOrganizationService projectOrganizationService;

    /**
     * 查询项目组织列表
     */
    @ApiOperation("查询项目组织列表")
    @PreAuthorize("@ss.hasPermi('gs:projectOrganization:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectOrganizationSearchVO projectOrganization){
        startPage();
        List<ProjectOrganization> list = projectOrganizationService.selectProjectOrganizationList(BeanUtil.copyProperties(projectOrganization,ProjectOrganization.class));
        return getDataTable(Convert.convert(new TypeReference<List<ProjectOrganizationResultVO>>() {},list));
}

    /**
     * 导出项目组织列表
     */
    @ApiOperation("导出项目组织列表")
    @PreAuthorize("@ss.hasPermi('gs:projectOrganization:export')")
    @Log(title = "项目组织", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectOrganization projectOrganization){
        List<ProjectOrganization> list = projectOrganizationService.selectProjectOrganizationList(projectOrganization);
        List<ProjectOrganizationExportVO> exportList = Convert.convert(new TypeReference<List<ProjectOrganizationExportVO>>() {}, list);
        ExcelUtil<ProjectOrganizationExportVO> util = new ExcelUtil<ProjectOrganizationExportVO>(ProjectOrganizationExportVO.class);
        util.exportExcel(response, exportList, "项目组织数据");
    }

    /**
     * 获取项目组织详细信息
     */
    @ApiOperation("获取项目组织详细信息")
    @PreAuthorize("@ss.hasPermi('gs:projectOrganization:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(projectOrganizationService.selectProjectOrganizationById(id));
    }

    /**
     * 新增项目组织
     */
    @ApiOperation("新增项目组织")
    @PreAuthorize("@ss.hasPermi('gs:projectOrganization:add')")
    @Log(title = "项目组织", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectOrganizationCreateVO projectOrganization){
        return toAjax(projectOrganizationService.insertProjectOrganization(BeanUtil.copyProperties(projectOrganization,ProjectOrganization.class)));
    }

    /**
     * 修改项目组织
     */
    @ApiOperation("修改项目组织")
    @PreAuthorize("@ss.hasPermi('gs:projectOrganization:edit')")
    @Log(title = "项目组织", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectOrganizationUpdateVO projectOrganization){
        return toAjax(projectOrganizationService.updateProjectOrganization(BeanUtil.copyProperties(projectOrganization,ProjectOrganization.class)));
    }

    /**
     * 删除项目组织
     */
    @ApiOperation("删除项目组织")
    @PreAuthorize("@ss.hasPermi('gs:projectOrganization:remove')")
    @Log(title = "项目组织", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(projectOrganizationService.deleteProjectOrganizationByIds(ids));
    }
    /**
     * 导入项目组织
     */
    @Log(title = "项目组织", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<ProjectOrganizationImportVO> util = new ExcelUtil<>(ProjectOrganizationImportVO.class);
        List<ProjectOrganizationImportVO> projectOrganizationList = util.importExcel(file.getInputStream());
        String message = projectOrganizationService.imporIteminfo(Convert.convert(new TypeReference<List<ProjectOrganization>>() {},projectOrganizationList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<ProjectOrganizationImportVO> util = new ExcelUtil<>(ProjectOrganizationImportVO.class);
        util.importTemplateExcel(response,"项目组织");
    }
}
